public class Solution {
    public int goodNodes(TreeNode root) {
        return help(root, Integer.MIN_VALUE);
    }

    private int help(TreeNode root, int preMax){
        if(root == null) return 0;
        int count = 0;
        if(root.val >= preMax){
            preMax = root.val;
            count++;
        }
        count += help(root.left, preMax);
        count += help(root.right, preMax);
        return count;
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
